<template>
  <div class="app-container">

    <el-card v-permission="['admin']">
      <el-row>
        <el-form ref="queryForm" inline size="small" :model="queryForm">
          <el-form-item label="所属院系" prop="deptId">
            <el-select v-model="queryForm.deptId" placeholder="请选择所属院系" filterable>
              <el-option
                v-for="item in deptList"
                :key="item.id"
                :label="item.label"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所属专业" prop="majorId">
            <my-cascader v-model="queryForm.majorId" :options="majorTreeList" :level="2"></my-cascader>
          </el-form-item>
          <el-form-item label="课程名" prop="name">
            <el-input placeholder="请输入课程名" v-model="queryForm.name"></el-input>
          </el-form-item>
          <el-button icon="el-icon-search" size="small" type="primary" @click="onClickSearch">搜索</el-button>
          <el-button icon="el-icon-refresh" size="small" @click="resetQueryForm">重置</el-button>
        </el-form>
      </el-row>
    </el-card>
    <el-table
      v-loading="loading"
      :data="list"
      element-loading-text="加载中"
      border
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column align="center" label="ID" width="95" prop="id"></el-table-column>
      <el-table-column label="课程名" prop="name" width="300"></el-table-column>
      <el-table-column label="学分" prop="credit"></el-table-column>
      <el-table-column label="总学时" prop="hours"></el-table-column>
      <el-table-column label="课程人数" prop="sumNumber"></el-table-column>
      <el-table-column label="所属院系" prop="dept.name" width="200"></el-table-column>
      <el-table-column label="授课教师" prop="teacher.name"></el-table-column>
      <el-table-column label="状态" prop="status">
        <template slot-scope="scope">
          <status-show data-name="course_status" :value="scope.row.status"></status-show>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="selectCourse(scope.row.id)">选修此课程</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import {selectCourse} from "@/api/selection";
import {getElectiveCourseList} from "@/api/course";
import List from "@/mixins/list";
import {getDeptSimpleList, getMajorTreeList, getTeacherTreeList} from "@/api/dept";
import MyCascader from "@/components/MyCascader";
import {mapGetters, mapState} from "vuex";
import StatusShow from "@/components/StatusShow";

export default {
  components: {StatusShow, MyCascader},
  data() {
    return {
      queryListFun: getElectiveCourseList,
      queryForm: {
        deptId: null,
        majorId: null,
        name: ''
      },
      name: '课程',
      deptList: [],
      majorTreeList: [],
      teacherTreeList: []
    }
  },
  mixins: [
    List
  ],
  mounted() {
    getDeptSimpleList({}).then(res => {
      const data = []
      for(let i of res.data) {
        data.push({
          id: i.id,
          label: i.name
        })
      }
      this.deptList = data
    })
    getMajorTreeList().then(res => {
      const majorTreeList = []
      for (let i of res.data) {
        const children = []
        for (let j of i.majors) {
          children.push({
            id: j.id,
            value: j.id,
            label: j.name,
          })
        }
        majorTreeList.push({
          id: i.id,
          value: i.id,
          label: i.name,
          children
        })
      }
      this.majorTreeList = majorTreeList
    })
    getTeacherTreeList().then(res => {
      const teacherTreeList = []
      for (let i of res.data) {
        const children = []
        for (let j of i.teachers) {
          children.push({
            id: j.id,
            value: j.id,
            label: j.name,
          })
        }
        teacherTreeList.push({
          id: i.id,
          value: i.id,
          label: i.name,
          children
        })
      }
      this.teacherTreeList = teacherTreeList
    })
  },
  computed: {
    ...mapGetters(['enums'])
  },
  methods: {
    selectCourse(courseId) {
      selectCourse(courseId).then(res => {
        this.$message({
          message: res.msg,
          type: 'success'
        });
      })
    }
  }
}
</script>

<style scoped>

</style>
